<?php

namespace Home\Controller;

/**
 * 提问控制器
 */
class AskController extends HomeController
{

    /**
     * 问答列表
     * [askLists description]
     * @return [type] [description]
     */
    public function askLists() {
        if(!UID){
            $this->error('您当前还未登录！',  U('User/login'));
        }
        $where['ask.status'] = 1;
        $where['a.status'] = 1;
        switch(I('type')){
            case 'hot':
                $order = 'view desc, id desc';
                break;
            case 'rec':
                $order = 'sort desc, id desc';
                break;
            default:
                $order = 'id desc';
        }
        $prefix = C('DB_PREFIX');
        $askList = M()
            ->field('ask.*, a.content as acontent, a.id as aid, a.agree ')
            ->table($prefix.'ask as ask')
            ->join($prefix."answer as a ON ask.id=a.ask_id")
//            ->join($prefix."answer as a ON ask.answer_id=a.id")
            ->where($where)->order($order)->limit(30)->select();
        FPC();
        FPC($askList);
        $this->assign('askList', $askList);
        $this->assign('title', '问题解答');
        $this->display();
    }
    
    /**
     * 回答详细
     * [answer description]
     * @param  integer $id     [description]
     * @param  string  $verify [description]
     * @return [type]          [description]
     */
    public function detail()
    {
        (I('id') && $askId = I('id')) || $this->error('问题不存在!');

        $where = array();
        $where['ask_id'] = $askId;
//        $where['a.status'] = 1;
        $prefix = C('DB_PREFIX');
        $answerLists = M()
            ->field('a.*, m.headimgurl, m.nickname ')
            ->table($prefix.'answer as a')
            ->join($prefix."member as m ON a.uid=m.uid")
            ->where($where)
            ->select();

        $where['a.status'] = 1;
        $answer = M()
            ->field('a.*, m.headimgurl, m.nickname ')
            ->table($prefix.'answer as a')
            ->join($prefix."member as m ON a.uid=m.uid")
            ->where($where)
            ->find();

        $where = array();
        $where['id'] = $askId;
        $where['status'] = array('gt', 0);
        $askArr = M('Ask')->where($where)->find();

        # 增加问题查看数量
        $result = M('Ask')->where($where)->setInc('view',1);

        $this->assign('answerLists', $answerLists);
        $this->assign('answer', $answer);
        $this->assign('askArr', $askArr);
        $this->assign('title', '回答详情');
        $this->display();
    }

    /**
     * 我要提问
     */
    public function ask(){
        if(IS_POST) {
            $score = I('score');
            # 验证积分是否充足
            $user = session('user_auth');
//            var_dump($user);
//            if($user['score']<$score){
//                $this->error('您当前的积分不足！',  U('Ask/askLists'));
//            }

            # END-验证积分是否充足
            $askId = I('id');
            $data['ask_id'] = $askId;
            $data['score'] = $score;
            $data['content'] = I('content');
            $data['uid']     = UID;
            $data['status'] = 1;
            $data['create_time'] = time();

            $res = M("Ask")->add($data);
            if(!empty($res)){
                # 扣积分
                action_log('ask_minus_score', 'Member', UID, UID, array('score'=>'-'.$score));
                D('Member')->login(UID);    // 更新用户session数据
                $this->success('提问成功!', U('User/faq'));
            }else{
                $this->error('提问失败！',  U('User/index'));
            }
        }
        $this->assign('title', '我要提问');
        $this->display();
    }

    /**
     * 回答问题
     */
    public function answer(){
        if(IS_POST) {
            $askId = I('id');
            $data['ask_id'] = $askId;
            $data['content'] = I('content');
            $data['uid']     = UID;
            $data['status'] = 0;
            $data['create_time'] = time();

            $res = M("answer")->add($data);
            if(!empty($res)){
                # 增加问题回答数量
                $where = array();
                $where['id'] = $askId;
                $result = M('Ask')->where($where)->setInc('count',1);
                # 增加用户回答数量, 暂无
                $this->success('回答成功！', U('detail', array('id'=>$askId)));
            }else{
                $this->error('回答失败！', U('User/index'));
            }
        }
        $this->assign('title', '回答问题');
        $this->display();
    }




}
